import axios from 'axios'
import qs from 'qs'
import store from '../vuex/store'
let baseUrl = ''
if (process.env.NODE_ENV === 'development') {
  baseUrl = 'http://localhost:8080/api'
} else if (process.env.NODE_ENV === 'production') {
  baseUrl = 'http://sign.51jili.com'
}
axios.interceptors.request.use((config) => {
  let token = localStorage.token
  store.commit('setShowLoad', true)
  if (token) {
    config.headers['Authorization'] = 'Bearer ' + token
  }
  return config
}, (error) => {
  return Promise.reject(error)
})

axios.interceptors.response.use(function (response) {
  store.commit('setShowLoad', false)
  return response
}, (error) => {
  return Promise.reject(error)
})
/**
 * 个人注册提交
 * @param {string} realname 名字
 * @param {string} cellphone 手机号
 * @param {string} idNumber 身份证号
 * @param {string} password 密码
 * @param {string} code 验证码
 */
export function registerSelf (realname, cellphone, idNumber, password, code) {
  return axios.post(baseUrl + '/register/individual', qs.stringify({
    realname: realname,
    cellphone: cellphone,
    id_number: idNumber,
    password: password,
    code: code
  }))
}
/**
 * 企业注册提交
 * @param {string} cellphone 手机号
 * @param {string} password 密码
 * @param {string} name 企业名
 * @param {string} organizationCode 统一信息代码
 * @param {string} code 验证码
 */
export function registerCom (cellphone, password, name, organizationCode, code, type) {
  return axios.post(baseUrl + '/register/firm', qs.stringify({
    cellphone: cellphone,
    password: password,
    company_name: name,
    organization_code: organizationCode,
    code: code,
    role: type
  }))
}
/**
 * 登陆提交
 * @param {string} phone 手机号
 * @param {string} password 密码
 */
export function login (phone, password) {
  return axios.post(baseUrl + '/api/login_check', qs.stringify({
    _username: phone,
    _password: password
  }))
}
/**
 * 修改密码提交
 * @param {string} oldPassword 旧密码
 * @param {string} newPassword 新密码
 * @param {string} code 验证码
 */
export function resetPassword (oldPassword, newPassword, code) {
  return axios.post(baseUrl + '/api/change_password', qs.stringify({
    old_password: oldPassword,
    new_password: newPassword,
    code: code
  }))
}
/**
 * 忘记密码提交
 * @param {string} phone 手机号
 * @param {string} newPassword 新密码
 * @param {string} code 验证码
 */
export function forgetPassword (phone, newPassword, code) {
  return axios.post(baseUrl + '/forget_password', qs.stringify({
    cellphone: phone,
    new_password: newPassword,
    code: code
  }))
}
/**
 * 短信发送接口
 * @param {string} phone 手机号
 */
export function message (phone) {
  return axios.post(baseUrl + '/get_verification_code', qs.stringify({
    cellphone: phone
  }))
}
/**
 * 获取需签合同
 */
export function getContract () {
  return axios.get(baseUrl + '/api/contract/get/unsigned')
}
/**
 * 获取已签合同
 */
export function getSigned () {
  return axios.get(baseUrl + '/api/contract/get/signed')
}
/**
 * 手动签署合同
 * @param {string} contractId 合同ID
 */
export function sign (contractId) {
  return axios.post(baseUrl + '/api/sign/manually', qs.stringify({
    contract_id: contractId
  }))
}
/**
 * 修改用户手机和邮箱地址
 * @param {string} cellphone 手机号
 * @param {string} email 邮箱
 * @param {string} code 验证码
 */
export function changeInfo (cellphone, email, code) {
  return axios.post(baseUrl + '/api/modify_info', qs.stringify({
    cellphone: cellphone,
    email: email,
    code: code
  }))
}
